package com.example.curriculum;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

//应用Volley框架解析JSON数据。
public class WeatherSsActivity extends AppCompatActivity {
    TextView cityname ,tianqi, quali,max,min;
    ImageView img;
    String url ="http://t.weather.sojson.com/api/weather/city/101210401";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather_ss);
        setTitle("");
        cityname = (TextView)findViewById(R.id.cityname);//城市名
        tianqi = (TextView)findViewById(R.id.tianqi);//天气
        quali=(TextView)findViewById(R.id.quali);
        max = (TextView)findViewById(R.id.max);//最高温
        min = (TextView)findViewById(R.id.min);//最低温
        img = (ImageView)findViewById(R.id.img);

        getData();
    }


    public void getData() {
        //1 排队
        RequestQueue mq = Volley.newRequestQueue(this);
        //2 轮到提要求
        JsonObjectRequest stringRequest= new JsonObjectRequest(
                url,
                null,
                new Response.Listener<JSONObject>(){
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONObject cityjson=response.getJSONObject("cityInfo");
                            String city=new String(cityjson.getString("city"));
                            JSONObject datajson=response.getJSONObject("data");
                            String quality=new String(datajson.getString("quality"));
                            JSONArray forecastjson=datajson.getJSONArray("forecast");
                            JSONObject forecastjson1=forecastjson.getJSONObject(0);
                            String type=new String(forecastjson1.getString("type"));
                            String high=new String(forecastjson1.getString("high"));
                            String low=new String(forecastjson1.getString("low"));

                            cityname.setText(city);
                            quali.setText("空气质量："+quality);
                            tianqi.setText("天气："+type);
                            max.setText(high);
                            min.setText(low);
                            if(type.equals("阴")){
                                img.setBackgroundResource(R.drawable.a9);
                            }


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener(){
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        cityname.setText("网页访问失败");
                    }
                }){  //将volley默认的ISO-8859-1格式转换为utf-8格式
            @Override
            protected Response<JSONObject> parseNetworkResponse(
                    NetworkResponse response) {
                try {  //jsonObject要和前面的类型一致,此处都是JSONObject
                    JSONObject jsonObject = new JSONObject(
                            new String(response.data, "UTF-8"));
                    return Response.success(jsonObject,
                            HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (Exception je) {
                    return Response.error(new ParseError(je)) ;
                }
            }
        };
        //3 结束服务，把数据带上
        mq.add(stringRequest);
    }

}

